package com.ynet.middleground.contract.service.impl;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.curator.shaded.com.google.common.base.Throwables;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.contract.bean.*;
import com.ynet.middleground.contract.dto.*;
import com.ynet.middleground.contract.model.PortAdminWebModel;
import com.ynet.middleground.contract.service.PortAdminWebService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Api(value = "内管使用服务", tags = {"内管使用服务"}, hidden = true)
@Path("port-web")
@Service(timeout = 6000, version = "0.0.1")
public class PortAdminWebServiceImpl implements PortAdminWebService, GenericService {

    @Autowired
    PortAdminWebModel portAdminWebModel;

    @POST
    @Path("template")
    @ApiOperation(value = "内管模版查询", notes = "内管模版查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortQueryContractReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortTemplateDTO>> listTemplate(PortQueryTemplateReqPager requestObj) {
        ServiceResult<List<PortTemplateDTO>> serviceResult = new ServiceResult<>();
        List<PortTemplateDTO> portTemplateDtos = null;
        try {
            portTemplateDtos = portAdminWebModel.listTemplates(requestObj);
            serviceResult.setResult(portTemplateDtos);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    org.apache.curator.shaded.com.google.common.base.Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(portTemplateDtos);
        return serviceResult;
    }

    @POST
    @Path("template-log")
    @ApiOperation(value = "内管模版日志查询", notes = "内管模版日志查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortTemplateLogQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortTemplateLogDTO>> portTemplateLog(PortTemplateLogQueryReq requestObj) {
        ServiceResult<List<PortTemplateLogDTO>> serviceResult = new ServiceResult<>();
        List<PortTemplateLogDTO> result = null;
        try {
            result = portAdminWebModel.portTemplateLog(requestObj);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    org.apache.curator.shaded.com.google.common.base.Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(result);
        return serviceResult;
    }

    @POST
    @Path("template-type")
    @ApiOperation(value = "内管模版统计查询", notes = "内管模版统计查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortTemplateTypeStatisticalReq", paramType = "body",
            required = true)
    @Override
    public ServiceResult<List<PortTemplateTypeStatisticalDTO>>
    portTemplateTypeStatistical(PortTemplateTypeStatisticalReq requestObj) {
        ServiceResult<List<PortTemplateTypeStatisticalDTO>> serviceResult = new ServiceResult<>();
        List<PortTemplateTypeStatisticalDTO> result = null;
        try {
            result = portAdminWebModel.portTemplateTypeStatistical(requestObj);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    org.apache.curator.shaded.com.google.common.base.Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(result);
        return serviceResult;
    }

    @POST
    @Path("archive-log")
    @ApiOperation(value = "内管档案日志查询", notes = "内管档案日志查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortArchiveLogQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortArchiveLogDTO>> portArchiveLogQuery(PortArchiveLogQueryReq requestObj) {
        ServiceResult<List<PortArchiveLogDTO>> serviceResult = new ServiceResult<>();
        List<PortArchiveLogDTO> result = null;
        try {
            result = portAdminWebModel.portArchiveLogQuery(requestObj);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    org.apache.curator.shaded.com.google.common.base.Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(result);
        return serviceResult;
    }

    @POST
    @Path("archive")
    @ApiOperation(value = "内管档案查询", notes = "内管档案查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortArchiveQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortArchiveDto>> portArchiveQuery(PortArchiveQueryReq requestObj) {
        ServiceResult<List<PortArchiveDto>> serviceResult = new ServiceResult<>();
        List<PortArchiveDto> result = null;
        try {
            result = portAdminWebModel.portArchiveQuery(requestObj);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    org.apache.curator.shaded.com.google.common.base.Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(result);
        return serviceResult;
    }

    @POST
    @Path("port-query")
    @ApiOperation(value = "内管合约查询", notes = "内管合约查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortQueryContractReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortContractDto>> portQueryContract(PortQueryContractReq requestObj) {
        ServiceResult<List<PortContractDto>> serviceResult = new ServiceResult<>();
        List<PortContractDto> contractDtos = null;
        try {
            contractDtos = portAdminWebModel.portQueryContract(requestObj);
            serviceResult.setResult(contractDtos);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(contractDtos);
        return serviceResult;
    }

    @POST
    @Path("port-contract-statistics")
    @ApiOperation(value = "内管合约统计查询", notes = "内管合约统计查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortQueryContractReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortContractSignStatisticsDto>>
    portContractStatistics(PortContractSignStatisticsReq requestObj) {

        ServiceResult<List<PortContractSignStatisticsDto>> serviceResult = new ServiceResult<>();
        try {
            serviceResult.setResult(portAdminWebModel.portContractStatistics(requestObj));
        } catch (IllegalArgumentException be) {
            serviceResult.setError("0001", be.getMessage());
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约签署统计服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        return serviceResult;
    }

    @POST
    @Path("port-contract-log")
    @ApiOperation(value = "内管合约日志查询", notes = "内管合约日志查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortQueryContractReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortContractLogDTO>> portContractLog(PortContractLogQueryReq requestObj) {
        ServiceResult<List<PortContractLogDTO>> serviceResult = new ServiceResult<>();
        List<PortContractLogDTO> contractDtos = null;
        try {
            contractDtos = portAdminWebModel.portContractLog(requestObj);
            serviceResult.setResult(contractDtos);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setPagerInfo(requestObj.getPagerInfo());
        serviceResult.setResult(contractDtos);
        return serviceResult;
    }

    @POST
    @Path("relation-contract")
    @ApiOperation(value = "关联合约查询请求", notes = "关联合约查询请求", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortQueryRelationContractReq", paramType = "body",
            required = true)
    @Override
    public ServiceResult<List<PortContractDto>> queryRelationContract(PortQueryRelationContractReq requestObj) {
        ServiceResult<List<PortContractDto>> serviceResult = new ServiceResult<>();
        List<PortContractDto> contractDtos = null;
        try {
            contractDtos = portAdminWebModel.queryRelationContract(requestObj);
            serviceResult.setResult(contractDtos);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(contractDtos);
        return serviceResult;
    }

    @POST
    @Path("get-contract-type")
    @ApiOperation(value = "获取合约类型", notes = "获取合约类型", hidden = true)
    @ApiImplicitParam(name = "requestObj", paramType = "body", required = true)
    @Override
    public ServiceResult<List<ContractTypeDTO>> getContractType() {
        ServiceResult<List<ContractTypeDTO>> serviceResult = new ServiceResult<>();
        List<ContractTypeDTO> contractTypes = null;
        try {
            contractTypes = portAdminWebModel.getContractType();
            serviceResult.setResult(contractTypes);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(contractTypes);
        return serviceResult;
    }

    @POST
    @Path("template-log-export")
    @ApiOperation(value = "内管模版日志导出查询", notes = "内管模版日志导出查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortTemplateLogQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortTemplateLogDTO>> portTemplateLogExport(PortTemplateLogQueryExportReq requestObj) {
        ServiceResult<List<PortTemplateLogDTO>> serviceResult = new ServiceResult<>();
        List<PortTemplateLogDTO> result = null;
        try {
            result = portAdminWebModel.portTemplateLogExport(requestObj);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    org.apache.curator.shaded.com.google.common.base.Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(result);
        return serviceResult;
    }

    @POST
    @Path("port-contract-log-export")
    @ApiOperation(value = "内管合约日志导出查询", notes = "内管合约日志导出查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortTemplateLogQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortContractLogDTO>> portContractLogExport(PortContractLogQueryExportReq requestObj) {
        ServiceResult<List<PortContractLogDTO>> serviceResult = new ServiceResult<>();
        List<PortContractLogDTO> contractDtos = null;
        try {
            contractDtos = portAdminWebModel.portContractLogExport(requestObj);
            serviceResult.setResult(contractDtos);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(contractDtos);
        return serviceResult;
    }

    @POST
    @Path("archive-log-export")
    @ApiOperation(value = "内管档案日志查询", notes = "内管档案日志查询", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "PortArchiveLogQueryReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<PortArchiveLogDTO>> portArchiveLogQueryExport(PortArchiveLogQueryExportReq requestObj) {
        ServiceResult<List<PortArchiveLogDTO>> serviceResult = new ServiceResult<>();
        List<PortArchiveLogDTO> result = null;
        try {
            result = portAdminWebModel.portArchiveLogQueryExport(requestObj);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "合约管理服务异常，case: {}",
                    org.apache.curator.shaded.com.google.common.base.Throwables.getStackTraceAsString(e));
            serviceResult.setError("ECCT0000", "服务异常，请联系系统管理员");
            return serviceResult;
        }
        serviceResult.setResult(result);
        return serviceResult;
    }
}
